package com.ydzt.fanka.server.utils;

import java.util.List;

import com.ydzt.fanka.server.domain.Node;

public class SqlUtil {
	private List<Node> nodes;
	public SqlUtil(){
		
	}

	public SqlUtil(List<Node> nodes){
		this.nodes = nodes;
	}
	
	public static String createInsertSqlPackage(){
		String sqlText = "";
		sqlText += "INSERT INTO TEST_TABLE_PACKAGE(SENDER,PACKAGE_ID,PACKAGE) INTO (?,?,?)";
		return sqlText;
	}
	
	/*
	 * 根据传入的表名和传入的节点，将传入的节点拼接为存储此节点的sql
	 * */
	public static String createInsertSql(String tablename,List<Node> nodes){
		StringBuilder sqlTextBuilder = new StringBuilder();
		//String sqlText = "";
		int count = nodes.size();
		sqlTextBuilder.append("INSERT INTO ");
		sqlTextBuilder.append(tablename);
		sqlTextBuilder.append("(");
		for(int i = 0;i < nodes.size(); i ++){
			if(i == count - 1){
				sqlTextBuilder.append(nodes.get(i).getName());
				sqlTextBuilder.append(")");
			}
			else{
				//sqlText += nodes.get(i).getName() + ",";
				sqlTextBuilder.append(nodes.get(i).getName());
				sqlTextBuilder.append(",");
			}
		}
		sqlTextBuilder.append("VALUES(");
		for(int i = 0; i < count;i ++){
			Node node = nodes.get(i);
			String dbType = node.getValueType();
			if(i != count - 1){
				if(dbType.equals("GB2312") || dbType.equals("STR") || dbType.equals("BCD")){
					//sqlText += "'" + ParamUtil.getNodeValueString(node) + "',";
					sqlTextBuilder.append("'");
					sqlTextBuilder.append(ParamUtil.getNodeValueString(node));
					sqlTextBuilder.append("',");
				}
				else
				{
					//sqlText += ParamUtil.getNodeValueString(node) + ",";
					sqlTextBuilder.append(ParamUtil.getNodeValueString(node));
					sqlTextBuilder.append(",");
				}
			}
			else{
				if(dbType.equals("GB2312") || dbType.equals("STR") || dbType.equals("BCD")){
					//sqlText += "'" + ParamUtil.getNodeValueString(node) + "')";
					sqlTextBuilder.append("'");
					sqlTextBuilder.append(ParamUtil.getNodeValueString(node));
					sqlTextBuilder.append("')");
				}
				else
				{
					//sqlText += ParamUtil.getNodeValueString(node) + ")";
					sqlTextBuilder.append(ParamUtil.getNodeValueString(node));
					sqlTextBuilder.append(")");
				}
			}
		}
		String sqlText = sqlTextBuilder.toString();
		sqlTextBuilder.delete(0, sqlTextBuilder.length());
		return sqlText;
	}
	
	public String createInsertSqlWithPreparedStatement(int iParamType){
		String sqlText = "";
		int count = nodes.size();
		String tablename = getTableName(iParamType);
		sqlText += "INSERT INTO " + tablename + "(";
		for(int i = 0;i < nodes.size(); i ++){
			if(i == count - 1){
				sqlText += nodes.get(i).getName() + ")";
			}
			else{
				sqlText += nodes.get(i).getName() + ",";
			}
		}
		sqlText += "VALUES(";
		for(int i = 0; i < count;i ++){
			Node node = nodes.get(i);
			
			if(i != count - 1){
				sqlText += ParamUtil.getNodeValueString(node) + "?,";
			}
			else{
				sqlText += ParamUtil.getNodeValueString(node) + "?)";
			}
		}
		return sqlText;
	}
	
	/*
	public static int insertRecordByPreparedStatement(int iParamType,List<Node> nodes){
		int flag = -1;//
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int count = nodes.size();
		String sqlText = createInsertSqlWithPreparedStatement(iParamType,nodes);
		try {
			conn = JdbcUtils.getConnectionForOracle();
			pstmt = conn.prepareStatement(sqlText);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	*/
	
	private static String getTableName(int paramType){
		String key = "P" + paramType;
		String tableName = ConfigFileUtil.getTableName(key);
		return tableName;
	}
}

